package com.finalist.controller.admin;

import java.text.Collator;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Locale;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.finalist.Message;
import com.finalist.entity.Departure;
import com.finalist.service.DepartureService;

@Controller("departureController")
@RequestMapping("/admin/departure")
public class DepartureController extends BaseController{
	
	@Resource(name = "departureServiceImpl")
	private DepartureService departureService;
	
	/**
	 * 列表 按中文首字母排序
	 * @param model
	 * @return
	 */
	@RequestMapping(value ="/list",method=RequestMethod.GET)
	public String list(ModelMap model){
		List<Departure> newDeparture = new ArrayList<Departure>();
 		List<Departure> departures = departureService.findAll();
 		newDeparture.addAll(departures);
		Collections.sort(newDeparture,new Comparator<Departure>(){
			public int compare(Departure o1, Departure o2) {
			       String s1 = o1.getName();
			            String s2 = o2.getName();
			            return Collator.getInstance(Locale.CHINESE).compare(s1, s2); 
			}
			        });
		model.addAttribute("departures",newDeparture);
		return "/admin/departure/list";
	}

	/**
	 * 添加
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/add", method = RequestMethod.GET)
	public String add(ModelMap model) {
		
		return "/admin/departure/add";
	}
	
	/**
	 * 保存
	 * @param departure
	 * @param redirectAttributes
	 * @return
	 */
	@RequestMapping(value = "/save",method = RequestMethod.POST)
	public String save(Departure departure,RedirectAttributes redirectAttributes){
		departureService.save(departure);
		addFlashMessage(redirectAttributes, SUCCESS_MESSAGE);
		return "redirect:list.jhtml";
	}
	
	/**
	 * 编辑
	 * @param id
	 * @param model
	 * @return
	 */
	@RequestMapping(value ="/edit",method = RequestMethod.GET)
	public String edit(Long id,ModelMap model){
		model.addAttribute("departure",departureService.find(id));
		return "/admin/departure/edit";
	}
	
	/**
	 * 更新
	 * @param departure
	 * @param redirectAttributes
	 * @return
	 */
	@RequestMapping(value = "/update",method = RequestMethod.POST)
	public String update(Departure departure,RedirectAttributes redirectAttributes){
		departureService.update(departure);
		addFlashMessage(redirectAttributes, SUCCESS_MESSAGE);
		return "redirect:list.jhtml";
	}
	
	/**
	 * 删除
	 */
	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	public @ResponseBody
	Message delete(Long id) {
		departureService.delete(id);
		return SUCCESS_MESSAGE;
	}
	
	@RequestMapping(value = "/check_name", method = RequestMethod.GET)
	public @ResponseBody
	boolean checkName(String name) {
		if (StringUtils.isEmpty(name)) {
			return false;
		}
		if (departureService.nameExists(name)) {
			return false;
		} else {
			return true;
		}
	}
}
